package com.yt.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.project.model.dto.bilibili.BilibiliQueryRequest;
import com.yt.project.model.entity.Bilibili;
import com.yt.project.model.vo.DescVO;
import com.yt.project.model.vo.BilibiliVO;
import com.yt.project.model.vo.TagDistVO;
import com.yt.project.service.BilibiliService;
import com.yt.project.mapper.BilibiliMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【bilibili】的数据库操作Service实现
* @createDate 2023-04-19 21:24:33
*/
@Service
public class BilibiliServiceImpl extends ServiceImpl<BilibiliMapper, Bilibili>
    implements BilibiliService{

    @Override
    public Page<BilibiliVO> pageBlibili(BilibiliQueryRequest bilibiliQueryRequest) {

        long current = 1;
        long pageSize = 10;
        if (bilibiliQueryRequest != null) {
            current = bilibiliQueryRequest.getCurrent();
            pageSize = bilibiliQueryRequest.getPageSize();
        }
        if (bilibiliQueryRequest == null) {
            bilibiliQueryRequest = new BilibiliQueryRequest();
        };
        QueryWrapper<Bilibili> bilibiliQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(bilibiliQueryRequest.getSearchText())) {
            bilibiliQueryWrapper.like("nickname", bilibiliQueryRequest.getSearchText());
        }

        if (bilibiliQueryRequest.getShare() != null) {
            bilibiliQueryWrapper.gt("share", bilibiliQueryRequest.getShare());
        }
        if (bilibiliQueryRequest.getComment() != null) {
            bilibiliQueryWrapper.gt("comment", bilibiliQueryRequest.getComment());
        }
        if (bilibiliQueryRequest.getZan() != null) {
            bilibiliQueryWrapper.gt("zan", bilibiliQueryRequest.getZan());
        }
        if (bilibiliQueryRequest.getFan() != null) {
            bilibiliQueryWrapper.gt("fan", bilibiliQueryRequest.getFan());
        }
        if (bilibiliQueryRequest.getTypes() != null && bilibiliQueryRequest.getTypes().size() > 0) {
            bilibiliQueryWrapper.in("ttype", bilibiliQueryRequest.getTypes());
        }

        if (bilibiliQueryRequest.getStartDate() != null && bilibiliQueryRequest.getEndDate() != null) {
            // 数据库startTime > bilibiliQueryRequest.getStartDate()  && 数据库endDate < bilbxxxx
            bilibiliQueryWrapper.gt("startTime", bilibiliQueryRequest.getStartDate());
            bilibiliQueryWrapper.lt("endTime", bilibiliQueryRequest.getEndDate());
        }

        Page<Bilibili> bilibiliPage = this.page(new Page<>(current, pageSize), bilibiliQueryWrapper);
        List<Bilibili> bilibilis = bilibiliPage.getRecords();
        List<BilibiliVO> bilibiliVOS = bilibilis.stream().map(bilibili -> {
            BilibiliVO bilibiliVO = new BilibiliVO();
            bilibiliVO.setId(bilibili.getId());
            bilibiliVO.setNickname(bilibili.getNickname());
            bilibiliVO.setNewPost(bilibili.getNewPost());
            bilibiliVO.setShare(bilibili.getShare());
            bilibiliVO.setComment(bilibili.getComment());
            bilibiliVO.setZan(bilibili.getZan());
            bilibiliVO.setFan(bilibili.getFan());
            bilibiliVO.setNewFan(bilibili.getNewFan());
            String type = bilibili.getTtype();
            List<String> types = new ArrayList<>();
            types.add(type);
            bilibiliVO.setTypes(types);
            bilibiliVO.setAvatar(bilibili.getAvatar());
            return bilibiliVO;
        }).collect(Collectors.toList());
        Page<BilibiliVO> bilibiliVOPage = new Page<>();
        bilibiliVOPage.setRecords(bilibiliVOS);
        bilibiliVOPage.setTotal(bilibiliPage.getTotal());
        return bilibiliVOPage;
    }

    @Override
    public DescVO getDescById(Integer id) {
        QueryWrapper<Bilibili> queryWrapper = new QueryWrapper<>();
        Bilibili bilibili = this.getById(id);
        String nickname = bilibili.getNickname();
        QueryWrapper<Bilibili> bilibiliQueryWrapper = new QueryWrapper<>();
        bilibiliQueryWrapper.eq("nickname", nickname);
        bilibiliQueryWrapper.orderByAsc("start_date");
        List<Bilibili> bilibiliList = this.list(bilibiliQueryWrapper);
        // 计算权重：
        List<String> dates = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        bilibiliList.forEach(b -> {
            // 0.2
            Long share = b.getShare();
            // 0.2
            Long comment = b.getComment();
            Long fan = b.getFan();
            Long newFan = b.getNewFan();
            Long zan = b.getZan();
            // 计算权重
            Double data = 0.2 * share + 0.2 * comment + 0.2 * fan + 0.2 * newFan + 0.2 * zan;
            Date startDate = b.getStartDate();
            Date endDate = b.getEndDate();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String date = formatter.format(startDate) + "-\n" + formatter.format(endDate);
            dates.add(date);
            datas.add(data.intValue() / 100);
        });
        DescVO descVO = new DescVO();
        descVO.setFan(String.valueOf(bilibili.getFan() > 10000 ? (bilibili.getFan() / 10000) + "w" : bilibili.getFan()));
        descVO.setComment(String.valueOf(bilibili.getComment() > 10000 ? (bilibili.getComment() / 10000) + "w" : bilibili.getComment()));
        descVO.setShare(String.valueOf(bilibili.getShare() > 10000 ? (bilibili.getShare() / 10000) + "w" : bilibili.getShare()));
        descVO.setId(bilibili.getId());
        descVO.setNickname(bilibili.getNickname());
        descVO.setAvatarUrl(bilibili.getAvatar());
        descVO.setDate(dates);
        descVO.setData(datas);
        return descVO;
    }

    @Override
    public List<TagDistVO> getTagsDist(Integer id) {
        //Bilibili bilibili = this.getById(id);
        QueryWrapper<Bilibili> bilibiliQueryWrapper = new QueryWrapper<>();
        bilibiliQueryWrapper.select("ttype");
        List<Bilibili> bilibiliList = this.list(bilibiliQueryWrapper);
        List<String> tagList = bilibiliList.stream().map(Bilibili::getTtype).collect(Collectors.toList());
        //System.out.println(tagList);
        Map<String, Long> map = new HashMap<>();
        for (String tag : tagList) {
            if (map.containsKey(tag)) {
                map.put(tag, map.get(tag) + 1);
            } else {
                map.put(tag, 1L);
            }
        }
        List<TagDistVO> tagDistVOList = new ArrayList<>();
        map.forEach((k, v) -> {
            TagDistVO tagDistVO = new TagDistVO();
            tagDistVO.setName(k);
            tagDistVO.setValue(v);
            tagDistVOList.add(tagDistVO);
        });
        return tagDistVOList;
    }
}




